﻿using System.Collections.Generic;

namespace Core
{
    public class SceneSystem : Component, ISceneSystem, IAwake, IUpdate
    {
        private readonly Dictionary<int, AbstractScene> m_dict = new();
        public IScene Curr { get; private set; }

        public virtual void Awake()
        {
        }

        public override void Dispose()
        {
            if (IsDisposed) return;

            foreach (var scene in m_dict.Values)
            {
                scene.Dispose();
            }
            m_dict.Clear();

            base.Dispose();
        }

        protected void Add(int id, AbstractScene scene)
        {
            if (m_dict.ContainsKey(id) || scene == null)
            {
                return;
            }
            m_dict[id] = scene;
        }

        protected void Add<T>(int id) where T : AbstractScene
        {
            T t = G.Factory.Create<T, int>(id);
            Add(id, t);
        }

        public void Switch(int id)
        {
            Curr?.Exit();
            Curr = m_dict[id];
            Curr.Enter();
        }

        public void Update()
        {
            Curr?.Update();
        }
    }
}
